# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=hypre
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.33.0
pkgrel=1
pkgdesc='A library for solving large, sparse linear systems on massively parallel computers (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.llnl.gov/casc/hypre/'
msys2_repository_url='https://github.com/hypre-space/hypre/'
msys2_documentation_url='https://hypre.readthedocs.io/'
msys2_references=(
  'aur: hypre'
)
license=('spdx:Apache-2.0 OR MIT')
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-superlu"
         $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-superlu_dist")
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi"))
source=("https://github.com/hypre-space/hypre/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-win32-check.patch"
        "0002-dsuperlu-cpu.patch")
sha256sums=('0f9103c34bce7a5dcbdb79a502720fc8aab4db9fd0146e0791cde7ec878f27da'
            '9402d6d7115dc5e9f65209281c6783d973fe7fdc184963a9b346686023737ba1'
            '6c13b611309957b0befe73e662b6410c99e2eea0e36e37e522082d4c2f41b2dd')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/hypre-space/hypre/pull/578
  apply_patch_with_msg \
    0001-fix-win32-check.patch

  apply_patch_with_msg \
    0002-dsuperlu-cpu.patch

}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} == aarch64 ]]; then
    _extra_config+=("-DHYPRE_ENABLE_DSUPERLU=OFF")
    _extra_config+=("-DHYPRE_ENABLE_MPI=OFF")
    
  else
    _extra_config+=("-DHYPRE_ENABLE_DSUPERLU=ON")
    _extra_config+=("-DHYPRE_ENABLE_MPI=ON")
  fi

  #Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DHYPRE_ENABLE_OPENMP=ON \
      -DHYPRE_ENABLE_SUPERLU=ON \
      -DTPL_DSUPERLU_INCLUDE_DIRS=${MINGW_PREFIX}/include/superlu_dist \
      -DTPL_DSUPERLU_LIBRARIES=${MINGW_PREFIX}/lib/libsuperlu_dist.a \
      -DTPL_SUPERLU_INCLUDE_DIRS=${MINGW_PREFIX}/include/superlu \
      -DTPL_SUPERLU_LIBRARIES=${MINGW_PREFIX}/lib/libsuperlu.a \
      ../${_realname}-${pkgver}/src

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DHYPRE_ENABLE_OPENMP=ON \
      -DHYPRE_ENABLE_SUPERLU=ON \
      -DTPL_DSUPERLU_INCLUDE_DIRS=${MINGW_PREFIX}/include/superlu_dist \
      -DTPL_DSUPERLU_LIBRARIES=${MINGW_PREFIX}/lib/libsuperlu_dist.dll.a \
      -DTPL_SUPERLU_INCLUDE_DIRS=${MINGW_PREFIX}/include/superlu \
      -DTPL_SUPERLU_LIBRARIES=${MINGW_PREFIX}/lib/libsuperlu.dll.a \
      ../${_realname}-${pkgver}/src

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install ./

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install ./

 # Remove path reference in cmake files
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/HYPRE/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYRIGHT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE-APACHE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-APACHE"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE-MIT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-MIT"
}
